cmake_minimum_required(VERSION 3.10)
project(SerialProject)

# 新增交叉编译选项
set(TARGET_PLATFORM "pc" CACHE STRING "Compile target (pc or debian)")
if(TARGET_PLATFORM STREQUAL "debian")
    set(CMAKE_SYSTEM_NAME Linux)
    set(CMAKE_SYSTEM_PROCESSOR aarch64)
    # 设置完整编译器路径
    set(CMAKE_C_COMPILER /usr/bin/aarch64-linux-gnu-gcc)
    set(CMAKE_CXX_COMPILER /usr/bin/aarch64-linux-gnu-g++)
    # 设置系统根目录（根据实际情况调整）
    set(CMAKE_SYSROOT /usr/aarch64-linux-gnu)
    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
endif()

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

option(DEBUG_BUILD "Build with debug information" OFF)
if(DEBUG_BUILD)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -g")
else()
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2")
endif()

if(TARGET_PLATFORM STREQUAL "debian")
    set(THREADS_PREFER_PTHREAD_FLAG ON)
    set(CMAKE_THREAD_LIBS_INIT "-lpthread")
endif()
find_package(Threads REQUIRED)

include_directories(${CMAKE_CURRENT_SOURCE_DIR})

# 修改编译器设置
if(TARGET_PLATFORM STREQUAL "debian")
    set(CMAKE_SYSTEM_NAME Linux)
    set(CMAKE_SYSTEM_PROCESSOR aarch64)
    set(CMAKE_C_COMPILER /usr/bin/aarch64-linux-gnu-gcc)
    set(CMAKE_CXX_COMPILER /usr/bin/aarch64-linux-gnu-g++)
    # 修正sysroot路径并添加库搜索路径
    set(CMAKE_SYSROOT /usr/aarch64-linux-gnu/libc)
    set(CMAKE_LIBRARY_PATH /usr/aarch64-linux-gnu/lib)
    set(CMAKE_FIND_ROOT_PATH /usr/aarch64-linux-gnu)
    # 添加链接器选项
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")
endif()

add_executable(a010_app
    main.cpp
    A010.cpp
    log.cpp
)

target_link_libraries(a010_app 
    PRIVATE
    ${CMAKE_THREAD_LIBS_INIT}
    rt
    atomic
)



# 修改线程选项配置
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
    if(TARGET_PLATFORM STREQUAL "pc")
        target_compile_options(a010_app PRIVATE -pthread)
    elseif(TARGET_PLATFORM STREQUAL "debian")
        target_compile_options(a010_app PRIVATE -pthread)
        target_link_libraries(a010_app PRIVATE pthread)  # 添加显式链接
    endif()

endif()